<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix" style="text-align: left;">
      <span>{{role == 3 ? "我的信息" :"学生信息"}}</span>
    </div>
    <div style="display: flex;align-items: center;margin-bottom:10px" v-if="role != 3">
      <el-input
        style="width: 250px;margin-right: 10px;"
        placeholder="请输入学生名称"
        v-model="name"
        clearable
      ></el-input>
      <el-button type="primary" @click="query">查询</el-button>
      <el-button type="primary" @click="add">新增学生</el-button>
    </div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="xueHao" label="学号"></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="sex" label="性别"></el-table-column>
      <el-table-column prop="subject" label="专业"></el-table-column>

      <el-table-column label="操作" width="120">
        <template scope="scope">
          <el-button type="text" size="small" @click="modify(scope.row)">修改</el-button>
          <el-button type="text" v-if="role != 3" size="small" @click="deletes(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="text-align:left;margin-top:25px"
      background
      layout="prev, pager, next"
      :total="totalCount"
      @current-change="getPage"
    ></el-pagination>

    <el-dialog :title="title" :visible.sync="show" width="500px">
      <el-form label-width="100px" size="small">
        <el-form-item label="姓名" :required="true">
          <el-input style="width: 280px;" :disabled="update" v-model="student.name"></el-input>
        </el-form-item>
        <el-form-item label="性别" :required="true">
          <el-radio v-model="student.sex" label="男">男</el-radio>
          <el-radio v-model="student.sex" label="女">女</el-radio>
        </el-form-item>
        <el-form-item label="专业" :required="true">
          <el-input style="width: 280px;" v-model="student.subject"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit()">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </el-card>
</template>

<script>
import { request } from "../request.js";
export default {
  name: "student",
  data() {
    return {
      name: "",
      role: localStorage.getItem("role"),
      tableData: [],
      pageNo: 1,
      pageSize: 10,
      totalCount: 0,

      title: "新增学生",
      show: false,
      update: false,
      student: {
        sex: "",
        name: "",
        subject: ""
      }
    };
  },

  created() {
    console.log(111);
    this.getData();
  },
  methods: {
    /**
     * 查询
     */
    query() {
      this.pageNo = 1;
      this.getData();
    },

    /**
     * 获取分页数据
     */
    getData() {
      let param = {
        name: this.name,
        pageNo: this.pageNo,
        pageSize: this.pageSize
      };

      request
        .request("/student/getStudentList", "POST", param, this)
        .then(data => {
          data = data.data;
          this.tableData = data.list;
          this.pageNo = data.page.currentPage;
          this.totalPage = data.page.totalCount;
          console.log(data);
        });
    },

    /**
     * 分页器翻页
     */
    getPage(page){
      this.pageNo = page;
      this.getData();
    },

    /**
     * 显示新增的模态框
     */
    add() {
      this.show = true;
      this.title = "新增学生";
    },

    /**
     * 提交新增/编辑
     */
    submit() {
      if (!this.student.name) {
        this.$message.error("请输入姓名");
        return;
      }
      if (!this.student.subject) {
        this.$message.error("请输入专业");
        return;
      }
      if (!this.student.sex) {
        this.$message.error("请选择性别");
        return;
      }

      request
        .request(
          this.update ? "student/update" : "student/add",
          "POST",
          this.student,
          this
        )
        .then(() => {
          this.student.name = "";
          this.student.subject = "";
          this.student.sex = "";
          this.$message.success(this.update ? "更新成功" : "新增成功");
          this.show = false;
          this.query();
        });
    },

    /**
     * 显示编辑的模态框
     */
    modify(student) {
      this.student = student;
      this.update = true;
      this.show = true;
      this.title = "学生信息编辑";
    },

    /**
     * 删除
     */
    deletes(id) {
      request.request("student/delete", "POST", { id: id }, this).then(() => {
        this.$message.success("删除成功");
        this.query();
      });
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
